package com.lagou.common;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @date 2020/10/23
 **/
public class Common {

//    private final static Map<String, TreeSet<Long>> METHOD_REQUEST_TIME = new ConcurrentHashMap<>();
    private final static Map<String, ExpiringMap<Long, String>> METHOD_REQUEST_TIME = new ConcurrentHashMap<>();

    public static void put(String methodName, Long time){
        ExpiringMap<Long, String> timeMap = METHOD_REQUEST_TIME.get(methodName);
        if (timeMap == null) {
            timeMap = ExpiringMap.builder()
                    .maxSize(5000)
                    .expiration(60, TimeUnit.SECONDS)
                    .expirationPolicy(ExpirationPolicy.ACCESSED)
                    .variableExpiration()
                    .build();
            METHOD_REQUEST_TIME.put(methodName, timeMap);
        }
        synchronized (timeMap) {
            timeMap.put(time, null);
        }
    }

    public static Set<Map.Entry<String, ExpiringMap<Long, String>>> get(){
        return METHOD_REQUEST_TIME.entrySet();
    }
}
